from OWRpy import *
import redRi18n
_ = redRi18n.get_(package = 'base')

import signals 
import redRGUI 


class RedRabline(OWRpy): 
    globalSettingsList = ['commit']
    def __init__(self, **kwargs):
        OWRpy.__init__(self, **kwargs)
        
        if not self.require_librarys(["graphics"]):
            self.status.setText('R Libraries Not Loaded.')
            return
        
        # Python variables
        self.data = {}
        self.RFunctionParam_x = ''
        
        # Inputs
        self.inputs.addInput('id0', 'Model fit (lm)', signals.base.RModelFit, self.processdata)#, multiple = True)
        
        # Outputs
        self.outputs.addOutput("id0","AB Line", signals.plotting.RPlotAttribute)
        
        self.RFunctionParam_lty = redRGUI.base.comboBox(self.controlArea, label = 'Line types', displayLabel=True,
        items = [(1,'________'), (2,'- - - -'), (3,'........'), (4,'_._._._.'), (5,'__ __ __'), (6,'__.__.__.')])
        self.RFunctionParam_lcol = redRGUI.base.comboBox(self.controlArea, label = 'Line color', displayLabel=True,
        items = [(1,'Black'), (2,'Red'), (3,'Green'), (4,'Blue'), (5,'Cyan'), (6,'Magenta'), (7,'Yellow'), (8,'Gray')])
        self.RFunctionParam_lwd = redRGUI.base.spinBox(self.controlArea, label = "Line width:", min = 1,max=10, value = 1 )
        
        self.commit = redRGUI.base.commitButton(self.bottomAreaRight, _("Commit"), alignment=Qt.AlignLeft, 
        callback = self.commitFunction, processOnInput=True)

    def processdata(self, data):
        if data:
            self.RFunctionParam_x=data.getData()
            #self.data = data
            self.commitFunction()
        else:
            self.RFunctionParam_x=''
        
    def commitFunction(self):
        if str(self.RFunctionParam_x) == '':
            return
        
        injection = []
        if unicode(self.RFunctionParam_lty.currentId()) != '':
            string = 'lty='+unicode(self.RFunctionParam_lty.currentId())+''
            injection.append(string)
        if unicode(self.RFunctionParam_lcol.currentId()) != '':
            string = 'col='+unicode(self.RFunctionParam_lcol.currentId())+''
            injection.append(string)
        if unicode(self.RFunctionParam_lwd.value()) != '':
            string = 'lwd='+unicode(self.RFunctionParam_lwd.value())+''
            injection.append(string)

        inj = ','.join(injection)
        
        newPlotAtt = signals.plotting.RPlotAttribute(self, data = 'abline('+self.RFunctionParam_x+', '+inj+')')
        self.rSend("id0", newPlotAtt)